﻿using MaterialDesignThemes.Wpf;
using Modules.MSentEvent;
using Prism.Events;
using Prism.Ioc;
using Sorting.App.Models;
using Sorting.App.Models.Enum;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TouchKeyBoard;

namespace Sorting.App.Views
{
    /// <summary>
    /// LoginView.xaml 的交互逻辑
    /// </summary>
    public partial class LoginView : UserControl
    {


        IEventAggregator _ea;
        IContainerExtension _container;
        public LoginView(IEventAggregator ea,IContainerExtension container)
        {
            InitializeComponent();

            _ea = ea;
            _container=container;
            #region  消息事件总线订阅

            //登录成功
            _ea.GetEvent<ViewModelSentEvent>().Subscribe(LoginViewMin,
                arg => arg.Code.Equals(EnumMessageType.ME100));

            _ea.GetEvent<ViewModelSentEvent>().Subscribe(LoginSucess,
                         arg => arg.Code.Equals(EnumMessageType.ME101));

            _ea.GetEvent<LoadingSentEvent>().Subscribe(LogOut);



            #endregion


        }

        private void LogOut(int obj)
        {
            if (obj == 3)
            {
                var view = _container.Resolve<LoginView>();
                var result =  DialogHost.Show(view, "RootDialog");
            }
        }


        /// <summary>
        /// 最小化界面
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void LoginViewMin(MessageModel obj)
        {
            //this.WindowState = WindowState.Minimized;
        }

        #region 消息事件总线执行

        /// <summary>
        /// 登录成功执行
        /// </summary>
        /// <param name="obj"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void LoginSucess(MessageModel obj)
        {
            DialogHost.CloseDialogCommand.Execute(null,null);
            //this.Hide();
        }

        #endregion


        private KeyBoardWindow _keyBoard;
        private void txtPassword_GotFocus(object sender, RoutedEventArgs e)
        {
            _keyBoard = new KeyBoardWindow("密码：", typeof(PasswordBox), (PasswordBox)sender);
            //this.WindowStartupLocation = WindowStartupLocation.Manual;
            //this.Left = (SystemParameters.PrimaryScreenWidth / 2.0)- 240;
            //this.Top = 60;
            //this.ResizeMode = ResizeMode.NoResize;
            //this.Topmost = true;
            _keyBoard.Show();
            Keyboard.ClearFocus();

        }

        private void txtPassword_LostFocus(object sender, RoutedEventArgs e)
        {

            //this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            //this.WindowStartupLocation = WindowStartupLocation.Manual;
            //this.Left = (SystemParameters.PrimaryScreenWidth / 2.0) - 240;
            //this.Top = (SystemParameters.PrimaryScreenHeight / 5.5);
            //this.ResizeMode = ResizeMode.NoResize;
            //this.Topmost = true;
            _keyBoard.Close();
        }

        private void txtUserName_GotFocus(object sender, RoutedEventArgs e)
        {
            _keyBoard = new KeyBoardWindow("账号：", typeof(TextBox), (TextBox)sender);
            //this.WindowStartupLocation = WindowStartupLocation.Manual;
            //this.Left = (SystemParameters.PrimaryScreenWidth / 2.0) - 240;
            //this.Top = 60;
            //this.ResizeMode = ResizeMode.NoResize;
            //this.Topmost = true;
            _keyBoard.Show();
            Keyboard.ClearFocus();
        }

        private void txtUserName_LostFocus(object sender, RoutedEventArgs e)
        {
            //this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            //this.WindowStartupLocation = WindowStartupLocation.Manual;
            //this.Left = (SystemParameters.PrimaryScreenWidth / 2.0)- 240;
            //this.Top = (SystemParameters.PrimaryScreenHeight / 5.5);
            //this.ResizeMode = ResizeMode.NoResize;
            //this.Topmost = true;
            _keyBoard.Close();
        }
    }
}
